package com.jic.manager.common.service;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.common.exception.ManagerException;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class ManagerBaseService {

    /** 查询接口通用 */
    protected <Q> RestResult<Q> executeQuery(Query<Q> query){
        RestResult<Q> result = new RestResult();
        try {
            long start = System.currentTimeMillis();
            //服务调用
            Q q = query.query();
            result.setCode("0000");
            result.setMsg("成功");
            result.setData(q);
            long end = System.currentTimeMillis();
            log.info("查询服务返回，用时,{},{}", end - start, JSON.toJSONString(result));
        }catch (ManagerException fe){
            log.info("查询异常", fe);
            result.setCode(fe.getCode());
            result.setMsg(fe.getMessage());
        }catch (Exception e){
            log.info("出现未知异常", e);
            result.setCode("9999");
            result.setMsg("未知异常");
        }
        return result;
    }

    protected interface Query<Q>{
        Q query() throws Exception;
    }
//
//    /** 非查询接口订单级别通用 */
//    protected <T,M> RestResult<T> executeOperate(OperateModel operateModel, BaseService.TemplateService<T,M> service){
//        RestResult<T> result = new RestResult();
//        try {
//            long start = System.currentTimeMillis();
//
//            //TODO 用redid做防重复提交处理，用请求流水号+操作动作做key，业务处理完毕后清除该数据
//            //如果业务正在处理中，返回请勿重复操作
//            //如果业务已经处理完毕，返回同样的结果，需要在operator表中加流水号（流水号和操作动作组合唯一）和返回结果
//
//            M model = service.init();
//            T t = null;
//            //加载模型
//            if(model instanceof OrderModel){
//                modelService.fillOrder((OrderModel) model);
//                operateModel.setOriginOrderStatus(new Integer(((OrderModel) model).getOrderStatus()));//处理前状态
//                //处理业务
//                t = service.process(model);
//                operateModel.setCurrentOrderStatus(((OrderModel) model).getOrderStatus());//处理后状态
//                operateModel.setResult(t);
//                modelService.saveOrder((OrderModel) model, operateModel);
//            }
//            else if(model instanceof OrderDeliverModel){
//                modelService.fillDeliver((OrderDeliverModel) model);
//                //处理业务
//                t = service.process(model);
//                operateModel.setResult(t);
//                modelService.saveDeliver((OrderDeliverModel) model, operateModel);
//            }
//            else if(model instanceof ParentOrderModel){
//                modelService.fillParentOrder((ParentOrderModel) model);
//                //处理业务
//                t = service.process(model);
//                operateModel.setResult(t);
//                modelService.saveParentOrder((ParentOrderModel) model, operateModel);
//            }
//            else if(model instanceof ShoppingCartModel){
//                modelService.fillShoppingCart((ShoppingCartModel) model);
//                t = service.process(model);
//                operateModel.setResult(t);
//                modelService.saveShoppingCart((ShoppingCartModel) model);
//            }
//            //返回结果
//            result.setCode(OrderEnum.SUCCESS.getCode());
//            result.setMsg(OrderEnum.SUCCESS.getMessage());
//            result.setData(t);
//            long end = System.currentTimeMillis();
//            log.info("操作服务返回，耗时{},{}", end -start, JSON.toJSONString(result));
//            return result;
//        }catch (OrderException fe){
//            log.info("订单异常", fe);
//            result.setCode(fe.getOrderEnum().getCode());
//            result.setMsg(fe.getMessage());
//        }catch (Exception e){
//            log.info("出现未知异常", e);
//            result.setCode(OrderEnum.UNKOWN_EXECEPTION.getCode());
//            result.setMsg(OrderEnum.UNKOWN_EXECEPTION.getMessage());
//        }
//        return result;
//    }
//
//    protected interface TemplateService<T,M>{
//
//        M init();
//
//        //处理并返回结果
//        T process(M model);
//
//    }

}
